/*
 * ebctool_help.c
 *
 * Copyright (c) 2001-2009 by Clunix Inc. All Rights Reserved.
 */
#include "config.h"

#include "ebctool.h"

#define N_HELPS	(sizeof(help_infos) / sizeof(struct help_info))

static void
show_help_connect(void)
{
	printf(
"connect <mdname> [ <server ip> [ <port>] ]: connect EBC\n"
"    <mdname>: EBC module name\n"
"    <server ip>: EBC server IP\n"
"    <port>: EBC server port\n"
	);
}

static void
show_help_peercon(void)
{
	printf(
"peercon <peer address> [ <timeout> ]: establish peer connection via EBC\n"
"    <peer address>: peer address. pa format like as pa/./peer\n"
"    <timeout>: timeout in msecs\n"
	);
}

static void
show_help_call(void)
{
	printf(
"call <target> <arg>: do EBC call\n"
"    <target>: PA string\n"
"    <arg>: ASR\n"
"    eg) call pa/./ebcd/echo test <str>test\n"
	);
}

static void
show_help_call1(void)
{
	printf(
"call1 <target> <asr>: do EBC call oneway\n"
"  eg) call1 pa/./ebcd/echo <str>hello\n"
	);
}

static void
show_help_bcall(void)
{
	printf(
"bcall <target> <size in byte> [<count>]: do bulky EBC call\n"
	);
}

static void
show_help_tcall(void)
{
	printf(
"tcall <target> <timeout in msec> [<count>]: do timeout call\n"
	);
}

static void
show_help_send(void)
{
	printf(
"send <target> <asr>\n"
"  eg) send pa/./abc/test <str>hello\n"
	);
}

static void
show_help_recv(void)
{
	printf(
"recv <pattern>\n"
	);
}

static void
show_help_bsend(void)
{
	printf(
"bsend <target> <size in bytes> [ <count> ]\n"
	);
}

static void
show_help_brecv(void)
{
	printf(
"brecv <pattern> [<count>]\n"
	);
}

static void
show_help_peerbsend(void)
{
	printf(
"peerbsend <size in bytes> [ <count>]\n"
	);
}

static void
show_help_peerbrecv(void)
{
	printf(
"peerbrecv [<count>]"
	);
}

static void
show_help_moncb(void)
{
	printf(
"moncb {add|del} <target>\n"
	);
}

static void
show_help_route(void)
{
	printf(
"route {add|del} <addrspec>\n"
	);
}

#ifdef ECM_DEBUG

static void
show_help_pch(void)
{
	printf(
"pch { list | info }\n"
	);
}

static void
show_help_pktb(void)
{
	printf(
"pktb: show packet buffer status\n"
	);
}

static void
show_help_callinfo(void)
{
	printf(
"Usage:\n"
" callinfo: show EBC call information\n"
	);
}

static void
show_help_monitor(void)
{
	printf(
"Usage:\n"
" monitor: show EBC monitor information\n"
	);
}

static void
show_help_mtrace(void)
{
	printf(
"Usage:\n"
" mtrace: show mtrace information\n"
" mtrace on [ <level> ] : start mtrace\n"
" mtrace selfon [ <level> ]: start mtrace with self thread trace only\n"
" mtrace off : stop mtrace\n"
" mtrace report: report mtrace\n"
" mtrace clear: clear mtrace information\n"
" <level>: default 4\n"
	);
}

#endif

static struct help_info {
	const char	*context;
	void (*help_func)();
} help_infos[] = {
	{ "connect", show_help_connect },
	{ "peercon", show_help_peercon },
	{ "call", show_help_call },
	{ "call1", show_help_call1 },
	{ "bcall", show_help_bcall },
	{ "tcall", show_help_tcall },
	{ "send", show_help_send },
	{ "recv", show_help_recv },
	{ "bsend", show_help_bsend },
	{ "brecv", show_help_brecv },
	{ "peerbsend", show_help_peerbsend },
	{ "peerbrecv", show_help_peerbrecv },
	{ "moncb", show_help_moncb },
	{ "route", show_help_route },
#ifdef ECM_DEBUG
	{ "pch", show_help_pch },
	{ "pktb", show_help_pktb },
	{ "callinfo", show_help_callinfo },
	{ "monitor", show_help_monitor },
	{ "mtrace", show_help_mtrace }
#endif
};

static void
show_help_top(void)
{
	printf(
"------------\n"
"ebctool help\n"
"------------\n"
"quit: exit ebctool\n"
"help [<command>]: show help\n"
"\n"
"ebctool command list\n"
" * connect: ebc connect\n"
" * peercon: establish peer connection\n"
" * call: EBC call\n"
" * call1: EBC call oneway\n"
" * bcall: bulky EBC call\n"
" * tcall: timeout EBC call\n"
" * send: EBC send\n"
" * recv: EBC recv\n"
" * bsend: bulky EBC send\n"
" * brecv: bulky EBC receive\n"
" * peerbsend: bulky EBC peer send\n"
" * peerbrecv: bulky EBC peer receive\n"
" * moncb: register monitor callback\n"
" * route: route manipulation(maybe not supported)\n"
#ifdef ECM_DEBUG
" * pchinfo: show packet channel information\n"
" * pktb: show packet buffer information\n"
" * callinfo: show EBC call information\n"
" * monitor: show EBC monitor information\n"
" * mtrace: show memory trace information\n"
#endif
"\n"
        );
}

void
cmd_help(char *help_context)
{
	int	i;

	if (help_context == NULL) {
		show_help_top();
		return;
	}

	for (i = 0; i < N_HELPS; i++) {
		if (ecm_strcmp(help_context, help_infos[i].context) == 0) {
			help_infos[i].help_func();
			break;
		}
	}
	if (i == N_HELPS) {
		fprintf(stderr, "%s: no help information\n", help_context);
	}
	ecm_free(help_context);


}
